package org.twitter.connection.store;

import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;

public class TwitterTokenStore {
	private static final String SHARED_PREF_NAME = "TWITTER_KOREATRAVEL";
	private static final String TWITTER_TOKEN_PIN = "TWITTER_TOKEN_PIN";
	private static final String TWITTER_TOKEN_KEY = "TWITTER_TOKEN_KEY";
	private static final String TWITTER_TOKEN_SECRET = "TWITTER_TOKEN_SECRET";
	private SharedPreferences sharedPreferences;

	public TwitterTokenStore(Activity activity) {
		super();
		sharedPreferences = activity.getSharedPreferences(SHARED_PREF_NAME, Activity.MODE_PRIVATE);
	}
	public boolean storePIN(String pin){
		SharedPreferences.Editor insSharedPrefEditor = sharedPreferences.edit();
		insSharedPrefEditor.putString(TWITTER_TOKEN_PIN, pin);
		Log.d("twitter","TwitterTokenStore pin store : "+pin);
		return insSharedPrefEditor.commit();
	}
	public boolean storeToken(AccessToken token) {
		SharedPreferences.Editor insSharedPrefEditor = sharedPreferences.edit();
			insSharedPrefEditor.putString(TWITTER_TOKEN_KEY, token.getToken());
			insSharedPrefEditor.commit();
			insSharedPrefEditor.putString(TWITTER_TOKEN_SECRET, token.getTokenSecret());
		Log.d("twitter","TwitterTokenStore torken store : "+token.getToken()+"    "+token.getTokenSecret());
		return insSharedPrefEditor.commit();
	}
	public String findPIN(){
		String pin = sharedPreferences.getString(TWITTER_TOKEN_PIN, "");
		Log.d("twitter","TwitterTokenStore pin search : "+pin);
		return pin; 
	}
	public AccessToken findAccessToken() {
		AccessToken resultAccessToken = null;
		TwitterTokenKey keys= loadToken();
		if (keys.validateToken()) {
			resultAccessToken = new AccessToken(keys.getTokenKey(), keys.getTokenSecretKey());
		} else {
			Log.i("test",
					"Twitter_LoadAccessToken() ## There's no saved strToken, strTokenSecret data");
		}
		Log.d("twitter","TwitterTokenStore pin search : "+keys);
		return resultAccessToken;
	}
	
	private TwitterTokenKey loadToken() {
		String cunsumerKey = sharedPreferences.getString(TWITTER_TOKEN_KEY, "");
		String cunsumerSecretKey = sharedPreferences.getString(TWITTER_TOKEN_SECRET, "");
		return new TwitterTokenKey(cunsumerKey, cunsumerSecretKey);
	}
}
